﻿namespace Sidvall.WorkItemManager.ConsoleWorkItemProvider
{
    public static class Program
    {
        #region Public Members

        public static void Main(string[] args)
        {
            RegisterTypes();

            try
            {
                var workItemProvider = Sidvall.WorkItemManager.Workflow.WorkItemProviderFactory.CreateProviderAsync(args, Properties.Settings.Default.WorkItemId, CreateProvider).Result;
                workItemProvider.ExecuteAsync().Wait();
            }
            catch (System.Exception ex)
            {
                Sidvall.WorkItemManager.SystemContext.Current.LogManager.WriteAsync(ex, Sidvall.Logging.LogCategories.Application).Wait();
                throw ex;
            }
        }

        #endregion
        #region Private Members

        #region CreateProvider

        private static Sidvall.WorkItemManager.Workflow.IWorkItemProvider CreateProvider()
        {
            var workItemProvider = Sidvall.SystemContext.Current.CodeManager.CreateInstance(Properties.Settings.Default.WorkItemProviderCodename) as Sidvall.WorkItemManager.Workflow.IWorkItemProvider;
            workItemProvider.Context = Properties.Settings.Default.WorkItemProviderContext;
            return workItemProvider;
        }

        #endregion
        #region RegisterTypes

        private static void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                //AccountManager = new Sidvall.Security.AccountManager(new Sidvall.WorkItemManager.Security.AccountProvider()),
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                AuthenticationStrategyManager = new Sidvall.Security.AuthenticationStrategyManager(new Sidvall.Security.AuthenticationStrategy()),
                //CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
                SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                SmsClientManager = new Sidvall.Net.Sms.SmsClientManager(new Sidvall.Net.Sms.SmsClient()),
                SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SqlManager = new Sidvall.Data.SqlManager(new Sidvall.Data.SqlServerProvider()),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                UserResponseStrategyManager = new Sidvall.Security.UserResponseStrategyManager(new Sidvall.Security.UserResponseStrategy()),
                //UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.Storage.LocalStorage()),

                // Client
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

                // Host specific
                CultureManager = new Sidvall.Globalization.CultureManager(new Sidvall.Globalization.CultureProvider()),
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.WorkItemManager.ConsoleWorkItemProvider.Configuration.ApplicationSettings()),
            };
            Sidvall.WorkItemManager.SystemContext.Current.InitializeAuthenticationStrategy();

            // Application specific
            Sidvall.WorkItemManager.SystemContext.Current.InitializeSqlServer(Properties.Settings.Default.WorkItemManagerConnectionString);
        }

        #endregion

        #endregion
    }
}
